<?php
/**
 * WooCommerce Cart Page Template
 * 购物车页面模板，基于HTML模板 cart-page.html
 * 
 * @package WP Bootstrap Woo
 */

defined('ABSPATH') || exit;

get_header(); ?>

<!-- breadcrumb start -->
<section class="breadcrumb-area">
    <div class="container">
        <div class="col">
            <div class="row">
                <div class="breadcrumb-index">
                    <?php woocommerce_breadcrumb(); ?>
                </div>
            </div>
        </div>
    </div>
</section>
<!-- breadcrumb end -->

<!-- cart page start -->
<section class="cart-page">
    <div class="container">
        <div class="cart-page-area">
            
            <?php if (WC()->cart->is_empty()) : ?>
                
                <!-- empty cart -->
                <div class="empty-cart">
                    <div class="empty-cart-content">
                        <div class="empty-cart-icon">
                            <i class="feather-shopping-cart"></i>
                        </div>
                        <h3>Your cart is empty</h3>
                        <p>Looks like you haven't added anything to your cart yet.</p>
                        <a href="<?php echo wc_get_page_permalink('shop'); ?>" class="btn-style1">Continue Shopping</a>
                    </div>
                </div>
                
            <?php else : ?>
                
                <form class="woocommerce-cart-form" action="<?php echo esc_url(wc_get_cart_url()); ?>" method="post">
                    
                    <div class="row">
                        <!-- cart items -->
                        <div class="col-12 col-lg-8">
                            <div class="cart-items">
                                <div class="cart-header">
                                    <h4>Shopping Cart</h4>
                                    <span class="cart-count"><?php echo WC()->cart->get_cart_contents_count(); ?> items</span>
                                </div>
                                
                                <div class="cart-table">
                                    <table class="shop_table shop_table_responsive cart woocommerce-cart-form__contents">
                                        <thead>
                                            <tr>
                                                <th class="product-thumbnail">Product</th>
                                                <th class="product-name">&nbsp;</th>
                                                <th class="product-price">Price</th>
                                                <th class="product-quantity">Quantity</th>
                                                <th class="product-subtotal">Total</th>
                                                <th class="product-remove">&nbsp;</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) : ?>
                                                
                                                <?php
                                                $_product = apply_filters('woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key);
                                                $product_id = apply_filters('woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key);
                                                
                                                if ($_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters('woocommerce_cart_item_visible', true, $cart_item, $cart_item_key)) :
                                                ?>
                                                
                                                <tr class="woocommerce-cart-form__cart-item <?php echo esc_attr(apply_filters('woocommerce_cart_item_class', 'cart_item', $cart_item, $cart_item_key)); ?>">
                                                    
                                                    <!-- product thumbnail -->
                                                    <td class="product-thumbnail">
                                                        <?php
                                                        $thumbnail = apply_filters('woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key);
                                                        
                                                        if (!$_product->is_visible()) {
                                                            echo $thumbnail;
                                                        } else {
                                                            printf('<a href="%s">%s</a>', esc_url($_product->get_permalink($cart_item)), $thumbnail);
                                                        }
                                                        ?>
                                                    </td>
                                                    
                                                    <!-- product name -->
                                                    <td class="product-name" data-title="Product">
                                                        <div class="product-info">
                                                            <?php
                                                            if (!$_product->is_visible()) {
                                                                echo wp_kses_post(apply_filters('woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key) . '&nbsp;');
                                                            } else {
                                                                echo wp_kses_post(apply_filters('woocommerce_cart_item_name', sprintf('<a href="%s">%s</a>', esc_url($_product->get_permalink($cart_item)), $_product->get_name()), $cart_item, $cart_item_key));
                                                            }
                                                            
                                                            // Meta data
                                                            echo wc_get_formatted_cart_item_data($cart_item);
                                                            
                                                            // Backorder notification
                                                            if ($_product->backorders_require_notification() && $_product->is_on_backorder($cart_item['quantity'])) {
                                                                echo wp_kses_post(apply_filters('woocommerce_cart_item_backorder_notification', '<p class="backorder_notification">' . esc_html__('Available on backorder', 'woocommerce') . '</p>', $product_id));
                                                            }
                                                            ?>
                                                        </div>
                                                    </td>
                                                    
                                                    <!-- product price -->
                                                    <td class="product-price" data-title="Price">
                                                        <?php echo apply_filters('woocommerce_cart_item_price', WC()->cart->get_product_price($_product), $cart_item, $cart_item_key); ?>
                                                    </td>
                                                    
                                                    <!-- quantity -->
                                                    <td class="product-quantity" data-title="Quantity">
                                                        <div class="quantity-wrap">
                                                            <?php
                                                            if ($_product->is_sold_individually()) {
                                                                $product_quantity = sprintf('1 <input type="hidden" name="cart[%s][qty]" value="1" />', $cart_item_key);
                                                            } else {
                                                                $product_quantity = woocommerce_quantity_input(
                                                                    array(
                                                                        'input_name'   => "cart[{$cart_item_key}][qty]",
                                                                        'input_value'  => $cart_item['quantity'],
                                                                        'max_value'    => $_product->get_max_purchase_quantity(),
                                                                        'min_value'    => '0',
                                                                        'product_name' => $_product->get_name(),
                                                                    ),
                                                                    $_product,
                                                                    false
                                                                );
                                                            }
                                                            
                                                            echo apply_filters('woocommerce_cart_item_quantity', $product_quantity, $cart_item_key, $cart_item);
                                                            ?>
                                                        </div>
                                                    </td>
                                                    
                                                    <!-- subtotal -->
                                                    <td class="product-subtotal" data-title="Total">
                                                        <?php echo apply_filters('woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal($_product, $cart_item['quantity']), $cart_item, $cart_item_key); ?>
                                                    </td>
                                                    
                                                    <!-- remove -->
                                                    <td class="product-remove">
                                                        <?php
                                                        echo apply_filters(
                                                            'woocommerce_cart_item_remove_link',
                                                            sprintf(
                                                                '<a href="%s" class="remove" aria-label="%s" data-product_id="%s" data-product_sku="%s"><i class="feather-trash-2"></i></a>',
                                                                esc_url(wc_get_cart_remove_url($cart_item_key)),
                                                                esc_html__('Remove this item', 'woocommerce'),
                                                                esc_attr($product_id),
                                                                esc_attr($_product->get_sku())
                                                            ),
                                                            $cart_item_key
                                                        );
                                                        ?>
                                                    </td>
                                                </tr>
                                                
                                                <?php endif; ?>
                                                
                                            <?php endforeach; ?>
                                        </tbody>
                                    </table>
                                </div>
                                
                                <div class="cart-actions">
                                    <div class="row">
                                        <div class="col-12 col-md-6">
                                            <div class="coupon">
                                                <input type="text" name="coupon_code" class="coupon-input" placeholder="Coupon code" value="<?php echo esc_attr(WC()->cart->get_applied_coupons() ? implode(', ', WC()->cart->get_applied_coupons()) : ''); ?>" />
                                                <button type="submit" class="btn-coupon" name="apply_coupon" value="Apply coupon">Apply Coupon</button>
                                            </div>
                                        </div>
                                        <div class="col-12 col-md-6">
                                            <div class="cart-update">
                                                <button type="submit" class="btn-update-cart" name="update_cart" value="Update cart">Update Cart</button>
                                                <a href="<?php echo wc_get_page_permalink('shop'); ?>" class="btn-continue-shopping">Continue Shopping</a>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <!-- cart totals -->
                        <div class="col-12 col-lg-4">
                            <div class="cart-summary">
                                <div class="cart-summary-header">
                                    <h4>Order Summary</h4>
                                </div>
                                
                                <div class="cart-totals">
                                    <?php do_action('woocommerce_before_cart_totals'); ?>
                                    
                                    <div class="cart-totals-content">
                                        
                                        <!-- subtotal -->
                                        <div class="cart-total-row">
                                            <span class="total-label">Subtotal:</span>
                                            <span class="total-value"><?php wc_cart_totals_subtotal_html(); ?></span>
                                        </div>
                                        
                                        <!-- shipping -->
                                        <?php if (WC()->cart->needs_shipping() && WC()->cart->show_shipping()) : ?>
                                            <div class="cart-total-row">
                                                <span class="total-label">Shipping:</span>
                                                <div class="total-value">
                                                    <?php wc_cart_totals_shipping_html(); ?>
                                                </div>
                                            </div>
                                        <?php endif; ?>
                                        
                                        <!-- coupons -->
                                        <?php foreach (WC()->cart->get_coupons() as $code => $coupon) : ?>
                                            <div class="cart-total-row cart-discount coupon-<?php echo esc_attr(sanitize_title($code)); ?>">
                                                <span class="total-label">Coupon: <?php echo esc_html($code); ?></span>
                                                <span class="total-value"><?php wc_cart_totals_coupon_html($coupon); ?></span>
                                            </div>
                                        <?php endforeach; ?>
                                        
                                        <!-- taxes -->
                                        <?php if (wc_tax_enabled() && !WC()->cart->display_prices_including_tax()) : ?>
                                            <?php if (get_option('woocommerce_tax_display_cart') === 'excl') : ?>
                                                <?php foreach (WC()->cart->get_tax_totals() as $code => $tax) : ?>
                                                    <div class="cart-total-row">
                                                        <span class="total-label"><?php echo esc_html($tax->label); ?>:</span>
                                                        <span class="total-value"><?php echo wp_kses_post($tax->formatted_amount); ?></span>
                                                    </div>
                                                <?php endforeach; ?>
                                            <?php else : ?>
                                                <div class="cart-total-row">
                                                    <span class="total-label">Tax included:</span>
                                                    <span class="total-value"><?php wc_cart_totals_taxes_total_html(); ?></span>
                                                </div>
                                            <?php endif; ?>
                                        <?php endif; ?>
                                        
                                        <!-- total -->
                                        <div class="cart-total-row cart-total-final">
                                            <span class="total-label">Total:</span>
                                            <span class="total-value"><?php wc_cart_totals_order_total_html(); ?></span>
                                        </div>
                                    </div>
                                    
                                    <?php do_action('woocommerce_after_cart_totals'); ?>
                                    
                                    <div class="proceed-to-checkout">
                                        <a href="<?php echo wc_get_checkout_url(); ?>" class="btn-checkout">
                                            Proceed to Checkout
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <?php wp_nonce_field('woocommerce-cart', 'woocommerce-cart-nonce'); ?>
                </form>
                
            <?php endif; ?>
            
        </div>
    </div>
</section>
<!-- cart page end -->

<script>
jQuery(document).ready(function($) {
    // 自动更新购物车数量
    $(document).on('change', '.qty', function() {
        var form = $(this).closest('form');
        form.find('[name="update_cart"]').trigger('click');
    });
    
    // 优惠券应用
    $('.btn-coupon').on('click', function(e) {
        e.preventDefault();
        var couponCode = $('.coupon-input').val();
        
        if (couponCode.trim() === '') {
            alert('Please enter a coupon code.');
            return;
        }
        
        $(this).closest('form').find('[name="apply_coupon"]').trigger('click');
    });
});
</script>

<?php get_footer(); ?>